{$R-}
{$D+}
(*
	PioneerFNum  -- a HyperCard user-defined command 
	to  get the frame number back from a Pioneer-LD-V6000  laser disc player.
	By Charles Kerns of Stanford University IRIS project.
	©Apple Computer, Inc. 1987
	All Rights Reserved.

	To compile and link this file using Macintosh Programmer's Workshop
	(HyperXCmd.p and XCmdGlue.inc must be accessible).

	pascal -w PioneerFNum.p
	link -m ENTRYPOINT -o HyperCommands -rt XCMD=16 -sn Main=PioneerFNum ¶
	  PioneerFNum.p.o "{MPW}"Libraries:interface.o

    then use ResEdit to copy the resulting XCMD from HyperCommands
	and paste it into the Home stack, or your own stack.
	(XCMD=11 Panasonic, =12 Hitachi, =13 Phillips, =14 PioneerLDV6000)
*)

{$S PioneerFNum } 	{ Segment name must be the same as the command name. }

UNIT DummyUnit;

INTERFACE

   USES MemTypes, QuickDraw, OsIntf, ToolIntf, HyperXCmd;
	
PROCEDURE EntryPoint(paramPtr: XCmdPtr);
	
IMPLEMENTATION


   TYPE Str19 = String[19];
   		Str31 = String[31];

   PROCEDURE PioneerFNum(paramPtr: XCmdPtr);							FORWARD;

   PROCEDURE EntryPoint(paramPtr: XCmdPtr);
   { entry point cannot have local procs, but forward routines can }
   BEGIN
     Pioneerfnum(paramPtr);
   END;

   PROCEDURE PioneerFNum(paramPtr: XCmdPtr);
   VAR tempStr: Str255;
	   OutrefNum: INTEGER;
	   inrefnum:integer;
	   InValue:longint; {the value read from player}
	   err: INTEGER;

     {$I XCmdGlue.inc }
	  
	 PROCEDURE Fail(errMsg: Str255); { set theResult and quit }
	 BEGIN
	   paramPtr^.returnValue := PasToZero(errMsg);
	   EXIT(PioneerFNum);
	 END;

	 Procedure SetHS(var handShake: SerShk);
	 begin
	 		 WITH handShake DO
			 BEGIN
			   fXon := 1;
			   fCTS := 1;
			   xon  := CHR(17);
			   xoff := CHR(19);
			   errs := 0;
			   evts := 0;
			   fInx := 0;
			 END;
    end; {setHS}
	
	 FUNCTION OpenSerial(NAME:str255):integer;
	 {return the ref num for the serial port that is named by parameter}
	 
	 VAR handShake: SerShk;
		 baudRate: INTEGER;
		 refnum:integer;
	 BEGIN
	   baudRate := 9600;
	   err := FSOpen(name,0,refNum);
	   
	   IF err = 0 THEN 
	     BEGIN
		 SetHS(handShake);
		   err := SerHShake(refNum,handShake);
		   IF err = 0 THEN 
			 err := Control(refNum,13,@baudRate);
		 END;
		 OpenSerial := refnum
	 END;


	 
	 
	 PROCEDURE CloseSerial(refnum:integer);
	 BEGIN
	   err := FSClose(refNum);
	 END;
	 
	 
	 PROCEDURE SendCommand(cmd: Str255;refnum:integer);
	 VAR count: LongInt;
	 BEGIN
	   count := Length(cmd);
	   err := FSWrite(refNum, count, Pointer(Ord(@cmd)+1));
	 END;
	 
	 Function ReadCommand(RefNum:integer): longint;
	 CONST
	 	kwaittime =120;
	 	count = 2;
	 
	 VAR 
	       LongCount,Startwait,BytesWaiting,Waitlength,instuff:longint;
		   
	 BEGIN	   
	   
	 LongCount := count;
	   instuff := 0;
	   
		StartWait := tickCount;
		repeat
		Err := SerGetBuf(RefNum,BytesWaiting);
		WaitLength := tickcount;
		until (byteswaiting >= count) or (WaitLength -StartWait  > kWaitTime);
		
	   if byteswaiting = count then err := FSread(refNum, LongCount, pointer(ord(@instuff)+2))
	   else instuff := 0;
	   
	   ReadCommand := instuff;
	 END;


   BEGIN
	 OutRefNum := OpenSerial('.AOUT');
	 IF err <> 0 THEN 
	   BEGIN
	   fail ('could not open serial port');
	   END;	 
	 InRefNum := OpenSerial('.AIN');
	IF err <> 0 THEN 
	 	BEGIN
	   		fail ('could not open serial port');
	 	END;
		
	 SendCommand('@E7',OutRefNum);		  
	 SendCommand('@D3',OutRefNum);
	 InValue:= ReadCommand(InRefNum);
	 
	tempstr := NumToStr(InValue);

	 CloseSerial(InRefNum);	 
	 CloseSerial(OutRefNum);
	
	paramPtr^.returnValue := PasToZero(tempstr);
   END;   


end.

